#include "scriptmodel.h"
#include <QDebug>
#include <QStringList>
#include <QDir>

ScriptModel::ScriptModel(QObject *parent) :
    QAbstractTableModel(parent)
{
}


int ScriptModel::rowCount(const QModelIndex & parent) const{

    return m_scripts.size();
}

int ScriptModel::columnCount(const QModelIndex & parent) const{
    return 2;
}

QVariant ScriptModel::data ( const QModelIndex & index, int role) const{


    if(role==Qt::DisplayRole||role==Qt::EditRole){
        int row = index.row();
        AED_Script *script = m_scripts[row];
        QVariant var;

        if(index.column()==0)
            var = script->GetName();
        else if(index.column()==1)
            var = script->GetPath();
        else
            return QVariant(QVariant::Invalid);
        return var;
    }
    return QVariant(QVariant::Invalid);
}

QVariant ScriptModel::headerData ( int section, Qt::Orientation orientation, int role) const{
    if(orientation==Qt::Horizontal){
        if (role==Qt::DisplayRole||role==Qt::EditRole){
            if(section==0)
                return "Name";
            else if(section==1)
                return "Path";
            else
                return QVariant(QVariant::Invalid);
        }else{
            return QVariant(QVariant::Invalid);
        }
    }else{
        if (role==Qt::DisplayRole||role==Qt::EditRole){
            QVariant h(QVariant::Int);
            h=section+1;
            return h;
        }else{
            return QVariant(QVariant::Invalid);
        }
    }
    return QVariant(QVariant::Invalid);
}

/*bool ScriptModel::setData(const QModelIndex &index, const QVariant &value, int role){
    int row = index.row();
    int column = index.column();
    AED_Image *img = m_manager->GetImage(row);
    if(index.isValid() && role==Qt::EditRole){
        if(column==2){
            int x_div = value.toInt();
            if(x_div>0)
                img->SetXDiv(x_div);
        }else if(column==3){
            int y_div = value.toInt();
            if(y_div>0)
                img->SetYDiv(y_div);
        }else{
            return false;
        }
        emit dataChanged(index,index);
        return true;
    }

    return false;
}*/

Qt::ItemFlags ScriptModel::flags(const QModelIndex &index) const{
    Qt::ItemFlags flags;

    flags = Qt::ItemIsEnabled|Qt::ItemIsSelectable;

    return flags;
}

void ScriptModel::LoadScript(const QString &path){

    AED_Script *script = new AED_Script;
    script->Load(path);
    m_scripts.push_back(script);

    emit layoutChanged();
    emit modified();

}

void ScriptModel::LoadScript(const QString &idpath, const QString &realpath){

    AED_Script *script = new AED_Script;
    script->Load(realpath);
    script->SetPath(idpath);
    m_scripts.push_back(script);

    emit layoutChanged();
    emit modified();

}

AED_Script *ScriptModel::GetScript(int index){
    return m_scripts[index];
}

AED_Script *ScriptModel::GetScript(const QString &path){
    for(int i=0;i<m_scripts.size();i++){
        if(m_scripts[i]->GetPath()==path)
            return m_scripts[i];
    }
    return 0;
}

void ScriptModel::EraseScript(int index){
    delete m_scripts[index];
    m_scripts.removeOne(m_scripts.at(index));
    emit layoutChanged();
    emit modified();
}

void ScriptModel::Clean(){
    for(int i=0;i<m_scripts.size();i++)
        delete m_scripts[i];
    m_scripts.clear();
    emit layoutChanged();
    emit modified();
}

void ScriptModel::Save(QTextStream &file){
    QList<AED_Script*>::iterator it ;
    for(it= m_scripts.begin();it!=m_scripts.end();++it){
        (*it)->Save(file);
    }
}

void ScriptModel::Save(QDomDocument &doc, QDomElement &header){
    QList<AED_Script*>::iterator it ;
    for(it= m_scripts.begin();it!=m_scripts.end();++it){
        (*it)->Save(doc,header);
    }
}

bool ScriptModel::Load(QDomElement &header, QString &scene_path){
    QDir dir(scene_path);
    QDomNodeList scripts = header.elementsByTagName("script");

    for(int i=0;i<scripts.count();i++){
        QDomElement ielem = scripts.at(i).toElement();
        QString path = dir.absoluteFilePath(ielem.attribute("path"));
        LoadScript(ielem.attribute("path"),path);
    }

    return true;
}

void ScriptModel::UpdatePaths(const QString &path){
    QDir dir(path);
    for(int i=0;i<m_scripts.size();i++){
        m_scripts[i]->SetPath(dir.relativeFilePath(m_scripts[i]->GetPath()));
    }
    emit modified();
}

